#ifndef VSCOMM_H
#define VSCOMM_H

#include "Global.h"


#define		BEEP_DELAY							150
#define   	PROMPT_DELAY                      	1000
#define	  	SHORT_PROMPT_DELAY					(PROMPT_DELAY/2)
#define   	DELAY_SCROLLING_TICKS             	600
#define   	PROMPT_DELAY_SECONDS              	2

#define CONNEXANT_MODEM_PARAM_SIZE	128	/* Connexant Modem needs bigger buffer */

#define MODEM_PARAM_SIZE  50
#define PABX_CODE_SIZE     4
#define PHONE_NUM_SIZE    30

/* Baud Rates For Communicating */
#define BAUD_300     0
#define BAUD_600     1
#define BAUD_1200    2
#define BAUD_2400    3
#define BAUD_4800    4
#define BAUD_9600    5
#define BAUD_19200   6
#define BAUD_38400   7
#define AUTO_BAUD    8


#define PRIMARY_LINE        0
#define SECONDARY_LINE      1
#define MAX_DIAL_LINES		2

#define  DTR_ON             0x01                                        
#define  DTR_OFF            0x00                                        
#define  RTS_ON             0x02
#define  RTS_OFF            0x00
#define  DTR_RTS_ON         0x03
#define  SetCtrl            0x0000  

#define SIGNAL_INFO         3
#define CTS_BIT             0x20

//#define DTR_DIAL            TRUE
//#define SYNC_CTS_CHECK      TRUE
#ifdef SYNC_CTS_CHECK
  #define VFI_SYNC_CONNECT    92
#endif  

#define CONNEXT_NO_LINE   87 

#define     MDM_CMD_WAIT_TIME           55          // the dreaded delay

#define MODEM_ENVIRONMENT_VARIABLE          "*MI"

#ifdef __arm
#define SOFTPAY_BASE_MDM_ENV_VAR1           "M1"
#define SOFTPAY_BASE_MDM_ENV_VAR2           "M2"
#define SYNC_MODEM_ENVIRONMENT_VARIABLE     "M3"
#define ASYNC_MODEM_ENVIRONMENT_VARIABLE    "M4"
#define MODEM_ENVIRONMENT_VARIABLE1         "M5"
#else
#define SOFTPAY_BASE_MDM_ENV_VAR1           "SP*MI1"
#define SOFTPAY_BASE_MDM_ENV_VAR2           "SP*MI2"
#define SYNC_MODEM_ENVIRONMENT_VARIABLE     "SP#SI"
#define ASYNC_MODEM_ENVIRONMENT_VARIABLE    "SP#AI"
#endif

#define DIALER3_PIPE_ID     "P:DIALER3"
#define CATM_PIPE_ID		"P:CATM"
//#define DIALER3_PIPE_ID   "P:DIALER"
//#define CATM_PIPE_ID		"P:SOFTPAY"


#define MDM_DEV_HANDLE      7

#define MDM_DEV_CONFLICT    -10101
#define MDM_DEV_ABORT       -10102

#define SEND_COMM_TIMEOUT	3000
#define RECV_COMM_TIMEOUT	3000
#define DEVICE_NAME_SIZE	32

#ifndef MSG_BUFF_SIZE
#define MSG_BUFF_SIZE 1028
#endif

typedef int (*PFI)();
typedef int (*PFS)();
typedef int (*PFI_VD)(void *);
typedef int (*PFI_PSZ)(char *);
typedef int (*PFI_VOID)(void);

typedef void  (*PFV_VD)(void *);
typedef int   (*PFI_PFOR_SEND)(void *, char *, int , VS_BOOL, int , long ); 
typedef int   (*PFI_PFOR_RES)(void *, char *, int ,  int , long ); 
typedef int   (*PFI_PFOR_BEGIN)(void *, int , VS_BOOL); 
typedef short (*PFI_VOID_CHAR)(void * , char *);
typedef int   (*PFI_PVOID_INT)(void * , int);
typedef int   (*PFI_SH_PSZ)(short, char *);
typedef int   (*PFI_VD_INT)(void *, int);
typedef int   (*PFI_INT_SH) (int ,unsigned short);
typedef int   (*PFI_INT_INT) (int ,unsigned int);
typedef int   (*PFI_VD_CHAR_BOOL) (void *, char *, VS_BOOL);

typedef struct syncdial_struct
{
	//short hModem;
	short shBaudRate;
	short shDataFormat;
	char  szPABXCode[PABX_CODE_SIZE+1];
	char  szPhoneNumber[PHONE_NUM_SIZE + 1];
	short shModemMode;
	short shSyncSwitchDelay;

#ifdef __arm
    int fDialType        : 1;    /* 0-Tone 1-Pulse */
    int fCommType        : 1;    /* 0-Bell 1-CCITT */
    int fTDK_MODEM       : 1;    /* OMNI3750 TDK-MODEM */
    int fAsync           : 1;    /* 0-Sync 1-Async */
    int fAbort           : 1;    /* 1-ABORT */
#else
    unsigned short fDialType        : 1;    // 0-Tone 1-Pulse 
    unsigned short fCommType        : 1;    // 0-Bell 1-CCITT 
    unsigned short fTDK_MODEM       : 1;    // OMNI3750 TDK-MODEM 
    unsigned short fAsync           : 1;    // 0-Sync 1-Async 
    unsigned short fAbort           : 1;    // 1-ABORT 
#endif

} SYNC_DIAL_OBJECT;

typedef struct dialer_resp
{

	//short hModem;
	short shMdmDialStatus;
    
} DIALER_RESP;

#define NO_COMM                     0
#define MODEM_COMM                  8
#define TCP_IP_COMM					9

#define LANDLINE_COMM				1
#define RS232_COMM					2
#define GSM_COMM					3
#define IP_COMM						4
#define WIFI_COMM					5
#define GPRS_COMM					6
#define CDMA_COMM					7

/* ISDN Mode Type Definitions, must be kept current with ICT.h */
#define ISDN_LOCAL_MODE             1
#define ISDN_TRANSPARENT_MODE       2
#define ISDN_BLIND_MODE             3

/* Inter Character Timeout for VISA Communication */
#define VS_RXPARITY             (-25)
#define VS_RXLRC                (-26)

#define RX_CONTROL              1
#define RX_RESPONSE             2
#define RX_MULTI_TRANS          3

#define NO_PARITY           0
#define EVEN_PARITY         1
#define ODD_PARITY          2

// xhayes_send_cmd adds AT at the beginning and /r at the end of the string
#define XHAYES_STRING_FRAME		3

// Data format
#define SDLC	7

#define fModem()                (obCommunication.inLinkType == MODEM_COMM)

typedef struct comm_struct {
	int             inLinkType;
	VS_BOOL			fLinkUp;
	void *Data;
	int (*inInitialize)(void *);
	int (*inBegin)(void *, int, VS_BOOL);
	int (*inCheck)(void *);
	int (*inSend)(void *, char *, int, VS_BOOL, int, long);
	int (*inReceive)(void *, char *, int, int, long);
	int (*inFlush)(void);
	int (*inEnd)(void *);
	int (*inDeinitialize)(void *);
	int (*inSendReceive)(char *, int, char *, int *); //LIEM
} COMM_OBJECT;

#define MODEM_RESP_TO       3
#define MAX_DIAL_LINES		2

#define	ASPEN_MODEM_ID		2
#define	TDK_MODEM_ID		3
#define	CONNEXANT_MODEM_ID	4

typedef struct modem_struct {
	int hModem;             /* Handle of the modem, local to this file */
	int inModemOpen;
	int inCurrentLine;      /* Primary or Secondary Line */
	short shSyncResultCode;
	int inDialAttempts[MAX_DIAL_LINES];
	VS_BOOL fNoPhoneNumber[MAX_DIAL_LINES];
} MODEM_OBJECT;

#define COMM_OBJECT_SIZE sizeof(COMM_OBJECT)
//#define LOGSYS_FLAG  //define this so logsys take effect
#include <logsys.h>

char *pchAlloc (unsigned unSize);
void vdFree (char *ptr);

void	vdGetPriAuthPhoneNumber(char *);
void	vdSetPriAuthPhoneNumber(char *);
void	vdGetSecAuthPhoneNumber(char *);
void	vdSetSecAuthPhoneNumber(char *);
void	vdGetPriSettlePhoneNumber(char *);
void	vdGetSecSettlePhoneNumber(char *);
short	shGetModemMode(void);
void	vdGetModemParam(char *);
short	shGetBaudRate(void);
short	shGetDataFormat(void);
short	shGetNumOfTries(void);
long	lnGetRedialDelay(void);
long	lnGetMultiTransDelay(void);
short	shGetCarrierTimeOut(void);
short	shGetRespTimeOut(void);
short	shGetInterCharTO(void);
short	shGetSyncSwitchDelay(void);
char*	szGetPABXCode(void);
void	vdGet3101HostAddr(char *);
VS_BOOL fGetReconnect(void);
VS_BOOL fGetModemFastConnect(void);
VS_BOOL fGetCommType(void);
VS_BOOL fGetBlindDial(void);
VS_BOOL fGetDialType(void);
VS_BOOL fGet3101CommType(void);

void vdInitializeModemComm (COMM_OBJECT *obCommunication);
int inInitModemCommunication (COMM_OBJECT *obCommunication);
int inOpenModemCommunication (COMM_OBJECT *obCommunication, int inOpCode);
int inBeginModemCommunication (COMM_OBJECT *obCommunication, int inCPTIndex, VS_BOOL fUsePrimaryLine);
int inCheckModemCommunication (COMM_OBJECT *obCommunication);
int inSendModemCommunication (COMM_OBJECT *obCommunication, char *pchSendBuff, int inSendSize, VS_BOOL fConfirmSend, int inTimeout, long lnMessage);
int inReceiveModemCommunication (COMM_OBJECT *obCommunication, char *pchReceiveBuff, int inReceiveSize, int inReceiveTimeout, long lnMessage);
int inFlushModemCommunication (void);
int inEndModemCommunication (COMM_OBJECT *obCommunication);
int inDeInitModemCommunication (COMM_OBJECT *obCommunication);

int inDefaultBaseInitModemEnvParam (MODEM_OBJECT *obModem);
int inDefaultTDKInitModemEnvParam (MODEM_OBJECT *obModem);
int inDefaultASPENInitModemEnvParam (MODEM_OBJECT *obModem);
int inDefaultPREDATORInitModemEnvParam (MODEM_OBJECT *obModem);
int inProcessPortError (COMM_OBJECT *obCommunication, int inErr);
void vdDispLineDialing( int inCurrLine );
int inGetPhoneNumber(MODEM_OBJECT *obModem, char *szPhoneNumber, int inCode);
int inModemDial (COMM_OBJECT *obCommunication, VS_BOOL fUsePrimaryLine);
int inSyncDialTask (MODEM_OBJECT *obModem, int inOpCode, VS_BOOL fAbort);

int inGetSendTimeOut(void);
int inGetRecvTimeOut(void);

//LIEM for TCP_IP_COMM
int	send_rev_cmd(char* sCmd, int iLenCmd, char* sRcv);
int Send_Rcv_Pack(char *SentBuff, int SentSz, char *RcvBuff, int *RcvSz);
int InitTcpIp(void);

void vdInitializeTcpIpComm (COMM_OBJECT *obCommunication);
int inInitTcpIpCommunication (COMM_OBJECT *obCommunication);
int inBeginTcpIpCommunication (COMM_OBJECT *obCommunication, int inCPTIndex, VS_BOOL fUsePrimaryLine);
int inCheckTcpIpCommunication (COMM_OBJECT *obCommunication);
int inSendTcpIpCommunication (COMM_OBJECT *obCommunication, char *pchSendBuff, int inSendSize, VS_BOOL fConfirmSend, int inTimeout, long lnMessage);
int inReceiveTcpIpCommunication (COMM_OBJECT *obCommunication, char *pchReceiveBuff, int inReceiveSize, int inReceiveTimeout, long lnMessage);
int inFlushTcpIpCommunication (void);
int inEndTcpIpCommunication (COMM_OBJECT *obCommunication);
int inDeInitTcpIpCommunication (COMM_OBJECT *obCommunication);

void vdInitializeUclComm (COMM_OBJECT *obCommunication);
int inGetModem (int h_modem, int inLocalTaskId);

int inInitializeComm (int inCommLinkType);
int inBeginComm(void);
int inEndComm(void);
int inReleaseComm(void);
int inDisplaySignalStrengthInterface(void);
int inDisplaySignalStrengthInternalInterface(void);

#define HOST_MODEM  "HOST.MODEM"
#define HOST_COMM   "HOST.COMM"

#endif   /* vscomm_H */
